package com.glory.hzlionpda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.bean.ByLotJobBean;

import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class ByLotJobInputAdapter extends BaseQuickAdapter<ByLotJobBean, BaseViewHolder> {

    public ByLotJobInputAdapter(@Nullable List<ByLotJobBean> data) {
        super(R.layout.adapter_by_lot_job_input, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, final ByLotJobBean item) {
        helper.setText(R.id.tvTitle, item.getPARAMETERDESC());
        helper.setText(R.id.tvNo, item.getLOTID());
        helper.setText(R.id.etValue, item.getPARAMETERVALUE());
        EditText editText = helper.getView(R.id.etValue);
        Editable b = editText.getText();
        editText.setSelection(b.length());
        setEditListener(editText, item);
    }

    private void setEditListener(EditText editText, final ByLotJobBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setPARAMETERVALUE(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
